<template>
  <el-dialog
      :title="dataForm.id==0 ?  '新增': '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible"
      width="45%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="身份证号码" prop="identityId">
        <el-input v-model="dataForm.identityId" placeholder="身份证号码"></el-input>
      </el-form-item>
      <el-form-item label="出生日期" prop="birthTime">
        <el-date-picker v-model="dataForm.birthTime" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" style="width: 200px;" >
            </el-date-picker>
      </el-form-item>
      
      <el-form-item label="性别" prop="gender">
        <el-select  v-model="dataForm.gender" clearable
                   placeholder="性别">
          <el-option
              v-for="item in genderType"
              :key="item.key"
              :label="item.value"
              :value="item.key">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="居住地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="居住地址"></el-input>
      </el-form-item>
      <el-form-item label="籍贯" prop="origin">
        <el-input v-model="dataForm.origin" placeholder="籍贯"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="tel">
        <el-input v-model="dataForm.tel" placeholder="电话"></el-input>
      </el-form-item>
      <el-form-item label="会员类型" prop="category">
        <el-select  v-model="dataForm.category" clearable
                   placeholder="会员类型">
          <el-option
              v-for="item in vipType"
              :key="item.key"
              :label="item.value"
              :value="item.key">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import service from "@/utils/httpRequest";

export default {
  data() {
    return {
      genderType: [
        {
          key: 0,
          value: '男'
        },
        {
          key: 1,
          value: '女'
        }
      ],
      vipType: [
        {
          key: 0,
          value: '会员'
        },
        {
          key: 1,
          value: '非会员'
        },
        {
          key: 2,
          value: '大学生'
        }
      ],
      // roleMap: [
      //   {
      //     key: 2,
      //     value: '普通管理员'
      //   },
      //   {
      //     key: 3,
      //     value: '普通用户'
      //   },
      //   {
      //     key: 4,
      //     value: '禁用'
      //   }
      // ],
      visible: false,
      dataForm: {
        id: 0,
        name: '',
        age: '',
        identityId: '',
        birthTime: '',
        gender: '',
        category: '',
        origin: '',
        address: '',
        tel: '',
        remark: ''
      },
      dataRule: {
        name: [
          {required: true, message: '姓名不能为空', trigger: 'blur'}
        ],
        
        gender: [
          {required: true, message: '性别不能为空', trigger: 'blur'}
        ],
        category: [
          {required: true, message: '会员类型不能为空', trigger: 'blur'}],
        tel: [
          {required: true, message: '电话不能为空', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          service({
            method: 'GET',
            url: `/lovehelp/fellow/info/${this.dataForm.id}`
          }).then(res => {
            if (res.code === 200) {
              this.dataForm = res.fellowVo;
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          service({
            method: 'POST',
            url: `/lovehelp/fellow/${!this.dataForm.id ? 'save' : 'update'}`,
            data: {
              id: this.dataForm.id,
              name: this.dataForm.name,
              identityId: this.dataForm.identityId,
              birthTime: this.dataForm.birthTime,
              gender: this.dataForm.gender,
              category: this.dataForm.category,
              origin: this.dataForm.origin,
              address: this.dataForm.address,
              tel: this.dataForm.tel,
              remark: this.dataForm.remark
            }
          }).then(res => {
            if (res.code === 200) {
              this.visible = false
              this.$emit('refreshDataList')
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                
              })
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>
